import { create } from 'zustand';
import {immer} from 'zustand/middleware/immer';
import { createJSONStorage, persist, devtools } from 'zustand/middleware';
const useFactorStore = create(
    // immer(
    //     devtools(
    //         persist()
    //     )
    // )

    devtools(
        persist(immer((set,get) => {
            return {
                tableData: [],
                selectFactorData: {},
                setTableData: async() => {
                    const data = await Promise.resolve([{id: 1, name: '张三'}, {id: 2, name: '李四11122222'}]);
                    set(state => {state.tableData = data});
                }
            }
        }), {
            name:'factorStore',
            storage: createJSONStorage(() => sessionStorage),
        })
    )
);

export const  setFactorData = async() => {
    // ajax
    const data = await Promise.resolve([{id: 1, name: '张三'}, {id: 2, name: '李四'}]);
    useFactorStore.setState(state => ({...state,tableData:data}));
}




export default useFactorStore;



(set,get) => {
    return {
        tableData: [],
        selectFactorData: {},
        setFactorData: async() => {
            const data = await Promise.resolve([{id: 1, name: '张三'}, {id: 2, name: '李四'}]);
            set(state => {state.tableData = data});
        }
    }
},{
    name:'factorStore',
    storage: createJSONStorage(() => sessionStorage),
}



// const createFactorStore = (set, get) => ({
//     tableData: [],
//     selectFactorData: {},
//     // 数据获取模块
//     actions: {
//       setFactorData: async () => {
//         const data = await fetchData();
//         set((state) => { state.tableData = data });
//       },
//     },
//     // 其他模块...
//   });
  
// 组件调用时：
// const { actions: { setFactorData } } = useFactorStore(); //  对actions 二次解构处理